package work.chenxr.security.service.impl;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import work.chenxr.security.service.SecurityUserDetailsService;
import work.chenxr.security.user.UserDetail;

import java.util.*;

@Service
public class SecurityUserDetailsServiceImpl implements SecurityUserDetailsService {
    @Override
    public UserDetails getUserDetails(UserDetail userDetail) {
        // 账号不可用
        if (userDetail.getStatus() == 0) {
            userDetail.setEnabled(false);
        }

        // 用户权限列表，假装这里是从数据库查出来的
        Set<String> authoritySet = new HashSet<>(Arrays.asList("user:list", "user:save", "user:update", "user:delete"));

        // 用户角色编码列表，假装这里是从数据库查出来的
        List<String> roleCodeList = new LinkedList<>(Arrays.asList("ADMINISTRATOR", "PRODUCT_MANAGER", "DEVELOPER", "TESTER"));
        roleCodeList.forEach(roleCode -> authoritySet.add("ROLE_" + roleCode.toUpperCase(Locale.ROOT)));

        // 写入权限列表
        userDetail.setAuthoritySet(authoritySet);

        return userDetail;
    }
}
